import bean.userBean;
import dao.userDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MybatisUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    SqlSession session=MybatisUtils.getSqlSession();
    userDao userDao = session.getMapper(userDao.class);
    @Test
//    搜索全部数据
    public void selectAll() {
        try {
            List<userBean> userList = userDao.AllUser();
            for (userBean bean : userList) {
                System.out.println(bean);
            }
        }finally {
            session.close();
        }
    }
    @Test
//    通过ID搜索
    public void selectId(){
        try {
            System.out.println(userDao.idUser(1));
        }finally {
            session.close();
        }
    }
    @Test
    public void insert(){
        userBean qq = new userBean(3, "QQ", "111111");
        try {
            int res=userDao.addUser(qq);
            System.out.println(res);
            if (res==1){
                System.out.println("插入成功");
                session.commit();
            }
        }finally {
            session.close();
        }
    }
    @Test
    public void updata(){
        userBean qq = new userBean(3, "QQ2", "111116");
        try {
            int res=userDao.updateUser(qq);
            System.out.println(res);
            if (res==1){
                System.out.println("修改成功");
                session.commit();
            }
        }finally {
            session.close();
        }
    }
    @Test
    public void delete(){
        try {
            int res=userDao.daleteUser(3);
            System.out.println(res);
            if (res==1){
                System.out.println("删除成功");
                session.commit();
            }
        }finally {
            session.close();
        }
    }
    @Test
    public void mapInsert(){
        try {
            Map<String,Object> userMap=new HashMap<>();
            userMap.put("userId",3);
            userMap.put("userName","AA");
            userMap.put("userPwd","444444");
            int res=userDao.mapAddUser(userMap);
            System.out.println(res);
            if (res==1){
                System.out.println("Map添加成功");
                session.commit();
            }
        }finally {
            session.close();
        }
    }
    @Test
    public void likeSelect(){
        try {
//            添加通配符%%
            List<userBean> res=userDao.selectLikeUser("%A%");
            if (res!=null){
                System.out.println("模糊查询成功");
                System.out.println(res);
            }
        }finally {
            session.close();
        }
    }
    @Test
//    resultMap搜索全部数据
    public void selectAllResultMap() {
        try {
            List<userBean> userList = userDao.AllUserResultMap();
            for (userBean bean : userList) {
                System.out.println(bean);
            }
        }finally {
            session.close();
        }
    }
    @Test
    //分页显示
    public void selectAllLimit() {
        Map<String,Integer> limit=new HashMap<>();
        limit.put("start",1);
        limit.put("last",1);
        try {
            List<userBean> userList = userDao.AllUserLimit(limit);
            for (userBean bean : userList) {
                System.out.println(bean);
            }
        }finally {
            session.close();
        }
    }
}
